"""用户会话类使用单例模式实现，确保在整个应用程序中只有一个实例存在。"""
class UserSession:
    _instance = None
    _session = {}

    def __new__(cls, *args, **kwargs):
        if cls._instance is None:
            cls._instance = super().__new__(cls, *args, **kwargs)
        return cls._instance

    def __init__(self):
        #防止重复初始化
        if not hasattr(self, 'initialized'):
            self.initialized = True
            #用户登录账号：用户信息
            self.user_dict={}

    def add_user(self,login_name,user_info):
        self.user_dict[login_name]=user_info

    def get_user(self,login_name):
        return self.user_dict.get(login_name)

    @classmethod
    def set_attribute(cls,key,value):
        cls._session[key]=value


    @classmethod
    def get_attribute(cls,key):
        return cls._session.get(key)

